package com.vanke.house.service.spider;

import com.vanke.house.model.HouseInfo;
import com.vanke.house.model.SaleHouse;
import com.vanke.house.service.HouseInfoService;
import com.vanke.house.service.SaleHouseService;
import com.vanke.house.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

/**
 * @author fangyu
 * @version v1.0.0
 * @date 2020/6/18 7:18 下午
 */
@Slf4j
@Service
public class HouseSpider {

    @Autowired
    private HouseInfoService houseInfoService;
    @Autowired
    private SaleHouseService saleHouseService;

    public void crawl(String url, String type) throws IOException {
        log.info("start");
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        // 模拟浏览器浏览（user-agent的值可以通过浏览器浏览，查看发出请求的头文件获取）
        httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36");
        CloseableHttpResponse response = httpclient.execute(httpGet);
        // 获取响应状态码
        int statusCode = response.getStatusLine().getStatusCode();
        try {
            HttpEntity entity = response.getEntity();
            if (statusCode == 200) {
                String html = EntityUtils.toString(entity, Consts.UTF_8);
                if ("ershou".equals(type)) {
                    log.info("start crawl ershou!");
                    parseSale(html);
                } else if ("chenjiao".equals(type)) {
                    log.info("start crawl chenjiao!");
                    parseDeal(html);
                } else {
                    log.info("type is not in now Spider");
                }
            }
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    }

    /**
     * 解析成交房源
     *
     * @param html
     */
    public void parseDeal(String html) {
        Document doc = null;
        doc = Jsoup.parse(html);
        Elements ulList = doc.select("ul[class='listContent']");
        Elements liList = ulList.select("li");
        for (Element item : liList) {
            String title = item.select("div[class='title']").text();
            String orientation = item.select("div[class='houseInfo']").text().trim();
            String floor = item.select("div[class='positionInfo']").text().trim();
            String dealDate = item.select("div[class='dealDate']").text().trim();
            String totalPrice = item.select("div[class='totalPrice']").text().trim().replace("万", "").trim();
            String price = item.select("div[class='unitPrice']").text().trim();
            String dealCycleTxt = item.select("span[class='dealCycleTxt']").text().trim();
            String[] deal = dealCycleTxt.split(" ");
            String listingPrice = deal[0].replace("挂牌", "").replace("万", "").trim();
            String dealCycle = deal[1];
            String link = item.select("a").attr("href").trim();
            HouseInfo houseInfo = new HouseInfo();
            houseInfo.setTitle(title);
            houseInfo.setOrientation(orientation);
            houseInfo.setFloor(floor);
            try {
                houseInfo.setDealTime(DateUtils.Str2Date(dealDate));
            } catch (ParseException e) {
                log.error("parse date error -> {}", dealDate);
            }
            houseInfo.setTotalPrice(Integer.parseInt(totalPrice));
            houseInfo.setPrice(price);
            houseInfo.setListingPrice(Integer.parseInt(listingPrice));
            houseInfo.setDealCycle(dealCycle);
            houseInfo.setLink(link);
            houseInfo.setCrawlTime(new Date());
            log.info("crawl house => {}", title);
            houseInfoService.add(houseInfo);
        }
    }

    /**
     * 解析在售房源
     *
     * @param html
     */
    public void parseSale(String html) {
        Document doc = null;
        doc = Jsoup.parse(html);
        Elements ulList = doc.select("ul[class='sellListContent']");
        Elements liList = ulList.select("li");
        log.info("size => {}", liList.size());
        for (Element item : liList) {
            String title = item.select("a[class='VIEWDATA CLICKDATA maidian-detail']").text();
            if ("".equals(title)) {
                break;
            }
            String img = item.select("img[class='lj-lazy']").attr("src").trim();
            String tag;
            try {
                tag = item.select("span[class='new tagBlock'").text().trim();
            } catch (Exception e) {
                log.error("get tag fail => {}", e.getMessage());
                tag = null;
            }
            String positionInfo = item.select("div[class='positionInfo']").text().trim();
            String[] houseInfo = item.select("div[class='houseInfo']").text().trim().split("\\|");
            log.info("houseInfo => {}", houseInfo);
            String floor = null;
            String buildYear = null;
            String houseType = null;
            String houseArea = null;
            String orientation = null;
            if (houseInfo.length > 0) {
                floor = houseInfo[0].trim();
                buildYear = houseInfo[1].trim();
                houseType = houseInfo[2].trim();
                houseArea = houseInfo[3].trim();
                orientation = houseInfo[4].trim();
            }


            String[] followInfo = item.select("div[class='followInfo']").text().trim().split("/");
            String followers = followInfo[0].trim();
            String publish_time = followInfo[1].trim();
            Date publishTime;
            if (publish_time.contains("今天")) {
                publishTime = new Date();
            } else if (publish_time.contains("天前发布")) {
                int days = Integer.parseInt(publish_time.substring(0, 1));
                publishTime = DateUtils.computeDate(new Date(), days);
            } else if (publish_time.contains("月前发布")) {
                int months = Integer.parseInt(publish_time.substring(0, 1));
                publishTime = DateUtils.computeDate(new Date(), months * 30);
            } else {
                publishTime = null;
            }

            String totalPrice = item.select("div[class='totalPrice']").select("span").text().trim();
            String price = item.select("div[class='unitPrice']").text().trim();
            String isFullTwo = item.select("div[class='tag']").text().trim();

            String link = item.select("a").attr("href").trim();
            SaleHouse saleHouse = new SaleHouse();
            saleHouse.setTitle(title);
            saleHouse.setHouseImg(img);
            saleHouse.setTag(tag);
            saleHouse.setPositionInfo(positionInfo);
            saleHouse.setFloor(floor);
            saleHouse.setBuildYear(buildYear);
            saleHouse.setHouseType(houseType);
            saleHouse.setHouseArea(houseArea);
            saleHouse.setFollowers(followers);
            saleHouse.setOrientation(orientation);
            saleHouse.setPublishTime(publishTime);
            saleHouse.setUnitPrice(price);
            saleHouse.setListingPrice(Integer.parseInt(totalPrice));
            saleHouse.setIsFullTwo(isFullTwo);
            saleHouse.setLink(link);
            saleHouse.setCrawlTime(new Date());
            log.info("crawl house => {}", title);
            saleHouseService.add(saleHouse);
        }
    }
}
